from typing import Optional
from schema.dao.drive_action import FeedbackInDb, FeedbackConverted, ViolationConverted, ViolationInDb, ComplaintInDb, ComplaintConverted


class PostProcessFeedback:
    """
    对 feedback 数据进行加工
    """

    @staticmethod
    def convert_feedback(feedback: FeedbackInDb) -> FeedbackConverted:
        """
        将存放于数据库中的 feedback 数据转换为前端所需的格式
        :param feedback:
        :return:
        """
        raw_dict = feedback.dict()
        raw_dict['user_type'] = PostProcessFeedback._convert_user_type(feedback.user_type)
        raw_dict['type'] = PostProcessFeedback._convert_type(feedback.user_type, feedback.type)
        raw_dict['source'] = PostProcessFeedback._convert_source(feedback.source)
        raw_dict['state'] = PostProcessFeedback._convert_state(feedback.state)
        raw_dict['del_state'] = PostProcessFeedback._convert_del_state(feedback.del_state)
        return FeedbackConverted(**raw_dict)

    @staticmethod
    def _convert_user_type(user_type: Optional[int]) -> Optional[str]:
        return {
            0: '乘客',
            1: '司机',
            2: '分时租赁用户'
        }.get(user_type)

    @staticmethod
    def _convert_type(user_type: Optional[int], type_: Optional[int]) -> Optional[str]:
        if user_type is None:
            return None
        if user_type == 1:
            return {
                1: '扣费申诉',
                2: '乘客物品丢失',
                3: '我要报备',
                4: '听单异常',
                5: '账号封禁',
                6: '成功率下降',
                7: '其他'
            }.get(type_)
        if user_type == 0:
            return {
                1: '司机以各种理由不来接我',
                2: '联系不上/找不到司机',
                3: '对司机服务不满意',
                4: '车辆或信息不符',
                5: '司机危险驾驶',
                6: '车内设施或环境卫生不满意',
                7: '绕路、不认路',
                8: '多扣款项',
                9: '支付失败',
                10: '其他'
            }.get(type_)
        if user_type == 2:
            return {
                1: '找不到车',
                2: '计费有问题',
                3: '租车押金相关问题',
                4: '开锁/锁门一直失败',
                5: '车辆无法启动',
                6: '导航问题',
                7: '找不到取车/还车站点',
                8: '其他'
            }.get(type_)
        return None

    @staticmethod
    def _convert_source(source: Optional[int]) -> Optional[str]:
        return {
            0: '网约车 APP',
            1: '分时租赁 APP',
            2: '分时租赁客服'
        }.get(source)

    @staticmethod
    def _convert_state(state: Optional[int]) -> Optional[str]:
        return {
            0: '处理中',
            1: '已处理'
        }.get(state)

    @staticmethod
    def _convert_del_state(del_state: Optional[int]) -> Optional[str]:
        return {
            0: '未删除',
            1: '已删除'
        }.get(del_state)


class PostProcessViolation:
    """
    对 violation 进行加工
    """
    @staticmethod
    def convert_violation(violation: ViolationInDb) -> ViolationConverted:
        raw_dict = violation.dict()
        raw_dict['v_type'] = PostProcessViolation._convert_v_type(violation.v_type)
        raw_dict['order_type'] = PostProcessViolation._convert_order_type(violation.order_type)
        raw_dict['handel_state'] = PostProcessViolation._convert_handel_state(violation.handel_state)
        raw_dict['pay_state'] = PostProcessViolation._convert_pay_state(violation.pay_state)
        return ViolationConverted(**raw_dict)

    @staticmethod
    def _convert_v_type(v_type: Optional[int]) -> Optional[str]:
        return {
            1: '违章',
            2: '事故'
        }.get(v_type)

    @staticmethod
    def _convert_order_type(order_type: Optional[int]) -> Optional[str]:
        return {
            1: '用户订单',
            2: '地服订单'
        }.get(order_type)

    @staticmethod
    def _convert_handel_state(handel_state: Optional[int]) -> Optional[str]:
        return {
            0: '未处理',
            1: '处理中',
            2: '已处理'
        }.get(handel_state)

    @staticmethod
    def _convert_pay_state(pay_state: Optional[int]) -> Optional[str]:
        return {
            0: '未交款',
            1: '已交款'
        }.get(pay_state)


class PostProcessComplaint:
    """
    对 complaint 进行加工
    """
    @staticmethod
    def convert_complaint(complaint: ComplaintInDb) -> ComplaintConverted:
        raw_dict = complaint.dict()
        raw_dict['complaint_source'] = PostProcessComplaint._convert_complaint_source(complaint.complaint_source)
        raw_dict['business_type'] = PostProcessComplaint._convert_business_type(complaint.business_type)
        raw_dict['state'] = PostProcessComplaint._convert_state(complaint.state)
        raw_dict['complaint_result'] = PostProcessComplaint._convert_complaint_result(complaint.complaint_result)
        raw_dict['del_state'] = PostProcessComplaint._convert_del_state(complaint.del_state)
        raw_dict['create_type'] = PostProcessComplaint._convert_create_type(complaint.create_type)
        return ComplaintConverted(**raw_dict)

    @staticmethod
    def _convert_complaint_source(complaint_source: Optional[int]) -> Optional[str]:
        return {
            1: '用户',
            2: '司机'
        }.get(complaint_source)

    @staticmethod
    def _convert_business_type(business_type: Optional[int]) -> Optional[str]:
        return {
            1: '快车',
            2: '出租车',
            3: '专车'
        }.get(business_type)

    @staticmethod
    def _convert_state(state: Optional[int]) -> Optional[str]:
        return {
            0: '待处理',
            1: '处理中',
            2: '已处理',
            3: '处理失败'
        }.get(state)

    @staticmethod
    def _convert_complaint_result(complaint_result: Optional[int]) -> Optional[str]:
        return {
            0: '未处理',
            1: '成立',
            2: '不成立',
            3: '其他'
        }.get(complaint_result)

    @staticmethod
    def _convert_del_state(del_state: Optional[int]) -> Optional[str]:
        return {
            0: '正常',
            1: '删除'
        }.get(del_state)

    @staticmethod
    def _convert_create_type(create_type: Optional[int]) -> Optional[str]:
        return {
            1: '系统创建',
            2: '人工创建',
            3: '客服系统创建'
        }.get(create_type)
